package com.zhouheng.springbootpython.common.utils;

import com.zhouheng.springbootpython.common.exception.BaseException;
import lombok.Cleanup;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.List;

public class FileUtil {

    private FileUtil(){

    }

    /**
     * 创建文件，存在了先删除
     *
     * @param path        文件路径 + 文件生成名
     * @param fileContent 文件内容
     * @author 周恒
     * @date 20181228 17:08:00
     * @since v1.0
     */
    public static void createFile(String path, String fileContent)throws IOException {
        //判断文件是否存在，如果存在则删除
        File fileold = new File(path);
        if(fileold.exists()){
            fileold.delete();
        }
        File file = fileold.getParentFile();
        if(!file.exists()){
            file.mkdirs();
        }
        @Cleanup FileWriter fw = new FileWriter(path, true);
        @Cleanup BufferedWriter bw = new BufferedWriter(fw);
        bw.write(fileContent);
    }

    /**
     * 获取文件内容.
     *
     * @param filePath 文件路径
     * @return 返回 file content
     * @throws IOException 描述抛出异常原因
     * @author 周恒
     * @date 20181229 10:49:27
     * @since v1.0
     */
    public static String getFileContent(String filePath) throws IOException {
        String content = "";
        File file = new File(filePath);
        if(!file.exists()){
            throw new BaseException("文件路径不存在:"+filePath);
        }
        String line;
        @Cleanup BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8"));
        while((line = buf.readLine())!=null){
            content += line;
        }
        return content;
    }

    /**
     * 删除多个文件
     *
     * @param path 描述此参数
     * @author 周恒
     * @date 20181229 11:01:20
     * @since v1.0
     */
    public static void delFiles(String... path){
        for(String p:path){
            delFile(p);
        }
    }

    /**
     * 删除单个文件
     *
     * @param path 描述此参数
     * @author 周恒
     * @date 20181229 11:00:14
     * @since v1.0
     */
    public static void delFile(String path){
        File file = new File(path);
        if(!file.exists()){
            throw new BaseException("要删除的目录不存在:"+path);
        }
        file.delete();
    }

    /**
     * 内容写入文件
     *
     * @param filePath 文件路径
     * @param sf       内容
     * @param append   追加还是覆盖：true追加，false覆盖
     * @throws IOException 描述抛出异常原因
     * @author 周恒
     * @date 20190110 14:45:37
     * @since v1.0
     */
    public static void writeFile(String filePath, String sf, boolean append)throws IOException{
        if(StringUtils.isAnyBlank(filePath)){
            throw new BaseException("文件路径不能为空");
        }
        File f = new File(filePath);
        if(!f.exists()){
            throw new BaseException("文件不存在");
        }
        @Cleanup FileWriter fw = new FileWriter(f, append);
        fw.write(sf);
        fw.flush();
    }

    /**
     * list转文件内容
     *
     * @param lists 描述此参数
     * @param line  多少个数据换行
     * @return 返回 string 描述此返回参数
     * @author 周恒
     * @date 20190110 16:39:58
     * @since v1.0
     */
    public static String listsToFileInfo(List<List<String>> lists, Integer line){
        StringBuilder sb = new StringBuilder();
        lists.forEach(list->{
            sb.append(listToFileInfo(list, line));
            sb.append("\n");
        });
        return sb.toString();
    }

    public static String listToFileInfo(List<String> list, Integer line){
        StringBuilder sb = new StringBuilder();
        int length = list.size();
        for(int i = 0; i<length; i++){
            if(null != line && 0 != i && 0 == i%line){
                sb.append("\n");
            }
            sb.append(list.get(i)+" ");
        }
        return sb.toString();
    }

}
